#!/usr/bin/perl 

  package WorkerThread;
  use threads;
  use threads::shared;
  use Thread::EventBus;
  
  sub new
  {
      my $class = shift;
      my $id = shift;
      my $self = {};
      bless $self, $class;
      
      $self->{ 'eb' } = Thread::EventBus->new( $self, 'worker#' . $id );
      $self->{ 'eb' }->set_procedure( 'method1', \&method1 );
      $self->{ 'eb' }->set_method( 'method2', \&method2 );
      $self->{ 'id' } = $id;
      
      $self->thread_start;
      return $self;
  }
  
  sub thread_start
  {
      my $self = shift;
      
      threads->create( \&threadloop, $self );
      return;
  }
  
  sub threadloop
  {
      my $self = shift;
      $self->{ 'eb' }->run;
  }

  sub method1
  {
      my $self = shift;
      my $data = shift;
      
      warn "Hello from method1(), thread $self->{id}!  Parameter: $data\n";
      return;
  }  
  
  sub method2
  {
      my $self = shift;
      my $data = shift;
      warn "Hello from method2(), thread $self->{id}!  Parameter: $data Returning the thread ID.\n";
      return $self->{ 'id' };
  }
  
  1;
  
  my $workerthread = WorkerThread->new( 1 );
  my $workerthread2 = WorkerThread->new( 2 );
  
  $workerthread->{eb}->method1( 'foo bar' );
  print "Result from method2: " . $workerthread2->{eb}->method2( 'baz' ) . "\n";

  sleep 1;
  